package runnerworm.core;

import java.awt.*;

/**
 * <p>Title: PolygonIntersectionChecker</p>
 * <p>Description: This will be a very helpful class that will
 * check if a polygon intersects another. Very useful when
 * you need to check if the tank representation is touching another
 * and many things like this.</p>
 * @author  Guilherme Mauro Germoglio
 */

class PolygonIntersectionChecker {

  /**
   * It checks if the intersection of to polygon is empty or not
   * @param polygon1 One polygon
   * @param polygon2 Another polygon
   * @return True if there's any intersection, false case not
   */
  public static boolean checkIntersection(Polygon polygon1, Polygon polygon2) {
    return ( (polygonContainsAnyOfThesePoints(polygon1, polygon2.xpoints,
                                              polygon2.ypoints,
                                              polygon2.npoints)) ||
             (polygonContainsAnyOfThesePoints(polygon2, polygon1.xpoints,
                                              polygon1.ypoints,
                                              polygon1.npoints)));

  }

  /**
   * It checks if any point of a set of points is or not contained the polygon
   * @param polygon1 The polygon I told you
   * @param polygon2_x_points An array with all the X points of another polygon
   * @param polygon2_y_points An array with all the Y points of another polygon
   * @param numberOfPoints The number of points the polygon 2 has
   * @return True if polygon1 contains these points, false case not
   */
  private static boolean polygonContainsAnyOfThesePoints(Polygon polygon1,
                                                  int[] polygon2_x_points,
                                                  int[] polygon2_y_points,
                                                  int numberOfPoints) {

    for (int k = 0; k < numberOfPoints; k++) {
      if (polygon1.contains(polygon2_x_points[k], polygon2_y_points[k]))
        return true;
    }
    return false;
  }

}